package com.dp.service.impl3;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dp.pojo.HotelssnVo;
import com.dp.pojo.JsonPage;
import com.dp.util.DataMap;
import com.dp.vo.NewsSnroom;


@Service
@Transactional
public class HotelssnServiceImpl extends BaseImpl {
	
	public boolean updateHotelssn(NewsSnroom snroom, String oldSn)
	{
        String sql= "update news_snroom set sn = '"+snroom.getSn()+"', spcode = '"+snroom.getSpcode()+"'" +
        		" where sn = '"+oldSn+"'";
        log.info(" update updateHotelssn excute sql :" +sql);
		try {
			return this.execUpdate(sql);
		} catch (Exception e) {
			log.info(" update updateHotelssn fail !!!" +e.getMessage());
			e.printStackTrace();
			return false;
		}
	}
	
	public boolean checkHotelssn(NewsSnroom snroom)
	{
        String sql = "select sn from news_snroom where sn = '"+snroom.getSn()+"'";
		log.info(" checkHotelssn excute sql :" +sql);
		try {
//			this.init();
//			this.rs1 = this.stmt.executeQuery(sql);
//			if (rs1.next())
//				return true;
//			else
				return false;
		} catch (Exception e) {
			log.info(" checkHotelssn fail !!!" +e.getMessage());
			e.printStackTrace();
			return false;
		} 
	}
	
	public boolean checkOccupancysn(String sn)
	{
        String sql = "select sn from t_occupancy where sn = '"+sn+"'";
		log.info(" checkHotelssn excute sql :" +sql);
		try {
//			this.init();
//			this.rs1 = this.stmt.executeQuery(sql);
//			if (rs1.next())
//				return true;
//			else
				return false;
		} catch (Exception e) {
			log.info(" checkOccupancysn fail !!!" +e.getMessage());
			e.printStackTrace();
			return false;
		} 
	}
	
	public int findAllNewsSnroomCount(DataMap hashMap) {
		String sn = (String)hashMap.get("sn");
		String spname = (String)hashMap.get("spname");
		String spcodeS=(String) hashMap.get("spcodeS");
		String sql = "SELECT count(n.sn) FROM news_snroom n join t_spcode t on n.spcode=t.spcode ";
		if(spcodeS!=null){
			sql += " AND t.spcode in ("+spcodeS+")";
		}
		if (sn != null && !"".equals(sn)) {
			sql += " and sn like '%" + sn + "%'";
		}
		if (spname != null && !"".equals(spname)) {
			sql += " and t.spname like '%" + spname + "%'";
		}
		int count = 0;
		try {
			log.info(" findAllNewsSnroomCount excute sql: " +sql);
			count = this.getOneInt();
		} catch (Exception e) {
			log.info(" findAllNewsSnroomCount fail !!!" +e.getMessage());
			e.printStackTrace();
		}
		return (count);
	}
	
	public List<HotelssnVo> findAllNewsSnroom(DataMap hashMap, int firstrow, int maxrow) {
		JsonPage jpage=(JsonPage)hashMap.get("jpage");
		String spname =(String)hashMap.get("spname");
		String sn = (String)hashMap.get("sn");
		String spcodeS=(String) hashMap.get("spcodeS");
		String sql = "SELECT n.sn,n.spcode,t.spname,t.spaddress FROM news_snroom n join t_spcode t on n.spcode=t.spcode ";
		if(spcodeS!=null){
			sql += " AND t.spcode in ("+spcodeS+")";
		}
		if (sn != null && !"".equals(sn)) {
			sql += " and sn like '%" + sn + "%'";
		}
		if (spname != null && !"".equals(spname)) {
			sql += " and spname like '%" + spname + "%'";
		}
		if (jpage != null && jpage.getSidx() != null
				&& !"".equals(jpage.getSidx())) {
			sql += " order by " + jpage.getSidx();
			sql += " " + jpage.getSord();
		}
		sql += " limit " +firstrow+","+maxrow;
		List<HotelssnVo> result = new ArrayList<HotelssnVo>();
		return result;
//		try {
//			this.init();
//			log.info(" findAll NewsSnroom excute sql: " +sql);
//			this.rs1 = this.stmt.executeQuery(sql);
//			while(rs1.next()){
//				HotelssnVo one = new HotelssnVo();
//				one.setSn(rs1.getString(1));
//				one.setSpcode(rs1.getString(2));
//				one.setSpname(rs1.getString(3));
//				one.setSpaddress(rs1.getString(4));
//				result.add(one);
//			}
//			return result;
//		} catch (Exception e) {
//			e.printStackTrace();
//			return null;
//		} finally{
//			this.closeAll();
//		}
	}
	
}